package com.zhike.b2b.user.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.StringTokenizer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.saas.biz.commen.CommGetId;
import com.saas.biz.commen.CommMethodMgr;
import com.saas.biz.commen.MD5;
import com.saas.biz.custMgr.CustInfo;
import com.saas.biz.dao.userDAO.UserExt;
import com.saas.sys.buffer.Buffers;
import com.saas.sys.exp.SaasApplicationException;
import com.zhike.b2b.base.BaseService;
import com.zhike.b2b.dao.Dbtable;

@Service
public class AccountInfo extends BaseService
{
  
  Logger log = LoggerFactory.getLogger(AccountInfo.class);
	
  Dbtable tradeQuery = new Dbtable();
  CommMethodMgr comm = new CommMethodMgr();
  Buffers inBuffer;
  Buffers outBuffer;
  ArrayList queryResult = new ArrayList();

  public void setTradeQuery(Dbtable paramDbtable)
  {
    this.tradeQuery = paramDbtable;
  }

  public Dbtable getTradeQuery()
  {
    return this.tradeQuery;
  }

  public void setOutBuffer(Buffers paramBuffers)
  {
    this.outBuffer = paramBuffers;
  }

  public Buffers getOutBuffer()
  {
    return this.outBuffer;
  }

  public ArrayList getQueryResult()
  {
    return this.queryResult;
  }

  public void setQueryResult(ArrayList paramArrayList)
  {
    this.queryResult = paramArrayList;
  }

  public void addAccountInfo(Buffers paramBuffers)
  {
    this.log.debug("addAccountInfo...");
    this.outBuffer = paramBuffers;
    this.inBuffer = paramBuffers;
    int i = -1;
    try
    {
      i = addAccountInfo(paramBuffers, 1);
    }
    catch (SaasApplicationException sae)
    {
      this.log.error(sae.getMessage());
    }
    if (i != 0)
    {
      this.outBuffer.setInt("RESULT_CODE", -1);
      this.outBuffer.setString("RESULT_INFO", "业务处理失败！");
    }
    else
    {
      this.outBuffer.setInt("RESULT_CODE", 0);
      this.outBuffer.setString("RESULT_INFO", "业务处理成功！");
    }
    this.log.info("退出addAuthorityInfo方法...");
  }

  public int addAccountInfo(Buffers paramBuffers, int paramInt)
    throws SaasApplicationException
  {
    UserExt localUserExt = new UserExt();
    CommGetId localcommGetId = new CommGetId();
    String str1 = localcommGetId.getTradeId("user_info", "user_id");
    MD5 localMD5 = new MD5();
    String str2 = localMD5.getMD5(paramBuffers.getString("PASSWD").getBytes());
    localUserExt.setParam(":VUSER_ID", str1);
    localUserExt.setParam(":VUSER_NAME", paramBuffers.getString("USER_NAME"));
    localUserExt.setParam(":VUSER_STATE", paramBuffers.getString("USER_STATE"));
    localUserExt.setParam(":VPASSWD", str2);
    localUserExt.setParam(":VUSER_TYPE", paramBuffers.getString("USER_TYPE"));
    localUserExt.setParam(":VCONTACT_NAME", paramBuffers.getString("CONTACT_NAME"));
    localUserExt.setParam(":VEMAIL", paramBuffers.getString("EMAIL"));
    localUserExt.setParam(":VREMARK", paramBuffers.getString("REMARK"));
    localUserExt.setParam(":VPHONE", paramBuffers.getString("PHONE"));
    localUserExt.setParam(":VCELLPHONE", paramBuffers.getString("CELLPHONE"));
    localUserExt.setParam(":VOPER_USER", paramBuffers.getString("USER_ID"));
    localUserExt.setParam(":VCUST_ID", paramBuffers.getString("CUST_ID"));
    localUserExt.setParam(":VRSRV_STR2", paramBuffers.getString("RSRV_STR2"));
    localUserExt.setParam(":VROLE_AUTH_ID", paramBuffers.getString("ROLE_AUTH_ID"));
    log.debug(paramBuffers.getString("ROLE_AUTH_ID") + "|||||||||||||||||||||||");
    String str3 = paramBuffers.getString("SESSION_CUST_NAME");
    CustInfo localCustinfo = new CustInfo();
    str3 = localCustinfo.getCustNameById(paramBuffers.getString("CUST_ID"));
    localUserExt.setParam(":VCUST_NAME", str3);
    this.tradeQuery.executeBy(localUserExt.insBy("INS_USER_INFO_BY_ALL_ACCOUNT"));
    return 0;
  }

  public ArrayList getAccountInfo(String paramString, int paramInt1, int paramInt2)
  {
    if (paramInt1 > 1)
      paramInt1 = (paramInt1 - 1) * paramInt2;
    else
      paramInt1 = 0;
    UserExt localUserExt = new UserExt();
    localUserExt.setParam(":VCUST_ID", paramString);
    ArrayList localArrayList = new ArrayList();
    localArrayList = localUserExt.selByList("GET_All_ACCOUNT_INFO", paramInt1, paramInt2);
    return localArrayList;
  }

  public int getAccountInfo(String paramString)
  {
    UserExt localUserExt = new UserExt();
    ArrayList localArrayList = new ArrayList();
    localUserExt.setParam(":VCUST_ID", paramString);
    localArrayList = localUserExt.selByList("GET_All_ACCOUNT_INFO");
    if ((localArrayList != null) && (localArrayList.size() > 0))
      return localArrayList.size();
    return 0;
  }

  public ArrayList getOneAccuntInfo(String paramString)
  {
    UserExt localUserExt = new UserExt();
    ArrayList localArrayList = new ArrayList();
    localUserExt.setParam(":VUSER_ID", paramString);
    localArrayList = localUserExt.selByList("GET_ONE_ACCOUNT_INFO");
    return localArrayList;
  }

  public ArrayList getOneAccuntInfo2(String paramString)
  {
    UserExt localUserExt = new UserExt();
    ArrayList localArrayList = new ArrayList();
    localUserExt.setParam(":VUSER_ID", paramString);
    localArrayList = localUserExt.selByList("GET_ONE_ACCOUNT_INFO2");
    return localArrayList;
  }

  public String getRoleName(String paramString)
  {
    String roleName = "";
    try
    {
      String sql = "select role_name from role where role_auth_id='" + paramString + "' group by role_auth_id";
      roleName = jt.queryForObject(sql, new RowMapper<String>()
    		  {
            public String mapRow(ResultSet rs, int arg1)
						throws SQLException {
				return rs.getString("role_name");
			}
    	 });
      }
    catch (Exception localSQLException)
    {
      localSQLException.printStackTrace();
    }
    return roleName;
  }

  public void updateAccountInfo(Buffers paramBuffers)
  {
    this.log.info("addAccountInfo...");
    this.outBuffer = paramBuffers;
    this.inBuffer = paramBuffers;
    int i = -1;
    try
    {
      i = updateAccountInfo(paramBuffers, 1);
    }
    catch (SaasApplicationException localSaasApplicationException)
    {
      this.log.info(localSaasApplicationException.getMessage());
    }
    if (i != 0)
    {
      this.outBuffer.setInt("RESULT_CODE", -1);
      this.outBuffer.setString("RESULT_INFO", "业务处理失败！");
    }
    else
    {
      this.outBuffer.setInt("RESULT_CODE", 0);
      this.outBuffer.setString("RESULT_INFO", "业务处理成功！");
    }
    this.log.info("退出addAuthorityInfo方法...");
  }

  public int updateAccountInfo(Buffers paramBuffers, int paramInt)
    throws SaasApplicationException
  {
    UserExt localUserExt = new UserExt();
    MD5 localMD5 = new MD5();
    String str = localMD5.getMD5(paramBuffers.getString("PASSWD").getBytes());
    localUserExt.setParam(":VUSER_ID", paramBuffers.getString("USER_ID"));
    localUserExt.setParam(":VUSER_NAME", paramBuffers.getString("USER_NAME"));
    localUserExt.setParam(":VUSER_STATE", paramBuffers.getString("USER_STATE"));
    localUserExt.setParam(":VUSER_TYPE", paramBuffers.getString("USER_TYPE"));
    localUserExt.setParam(":VCONTACT_NAME", paramBuffers.getString("CONTACT_NAME"));
    localUserExt.setParam(":VEMAIL", paramBuffers.getString("EMAIL"));
    localUserExt.setParam(":VREMARK", paramBuffers.getString("REMARK"));
    localUserExt.setParam(":VPHONE", paramBuffers.getString("PHONE"));
    localUserExt.setParam(":VCELLPHONE", paramBuffers.getString("CELLPHONE"));
    localUserExt.setParam(":VOPER_USER", paramBuffers.getString("OPER_USER"));
    localUserExt.setParam(":VRSRV_STR2", paramBuffers.getString("RSRV_STR2"));
    localUserExt.setParam(":VROLE_AUTH_ID", paramBuffers.getString("ROLE_AUTH_ID"));
    this.tradeQuery.executeBy(localUserExt.insBy("UPDATE_USER_INFO_BY_ALL"));
    return 0;
  }

  public void deleteAccountOne(Buffers paramBuffers)
  {
    this.log.info("进入deleteAccountOne方法...");
    this.outBuffer = paramBuffers;
    this.inBuffer = paramBuffers;
    int i = -1;
    String str = paramBuffers.getString("USER_ID");
    try
    {
      i = deleteAccountInfo(str);
    }
    catch (SaasApplicationException localSaasApplicationException)
    {
      this.log.info(localSaasApplicationException.getMessage());
    }
    if (i != 0)
    {
      this.outBuffer.setInt("RESULT_CODE", -1);
      this.outBuffer.setString("RESULT_INFO", "业务处理失败！");
    }
    else
    {
      this.outBuffer.setInt("RESULT_CODE", 0);
      this.outBuffer.setString("RESULT_INFO", "业务处理成功！");
    }
    this.log.info("退出deleteAccountOne方法...");
  }

  public void deleteAccountInfo(Buffers paramBuffers)
  {
    this.log.info("进入deleteAccountInfo方法...");
    this.outBuffer = paramBuffers;
    this.inBuffer = paramBuffers;
    int i = -1;
    String str1 = paramBuffers.getString("USER_ID");
    try
    {
      StringTokenizer localStringTokenizer = new StringTokenizer(str1, "|");
      String str2 = "";
      while (localStringTokenizer.hasMoreTokens())
      {
        str2 = localStringTokenizer.nextToken();
        i = deleteAccountInfo(str2);
      }
    }
    catch (SaasApplicationException localSaasApplicationException)
    {
      this.log.info(localSaasApplicationException.getMessage());
    }
    if (i != 0)
    {
      this.outBuffer.setInt("RESULT_CODE", -1);
      this.outBuffer.setString("RESULT_INFO", "业务处理失败！");
    }
    else
    {
      this.outBuffer.setInt("RESULT_CODE", 0);
      this.outBuffer.setString("RESULT_INFO", "业务处理成功！");
    }
    this.log.info("退出deleteAccountInfo方法...");
  }

  public int deleteAccountInfo(String paramString)
    throws SaasApplicationException
  {
    UserExt localUserExt = new UserExt();
    localUserExt.setParam(":VUSER_ID", paramString);
    this.tradeQuery.executeBy(localUserExt.insBy("DELETE_ACCOUNT_BY_USER_ID"));
    return 0;
  }

  public void LockAccount(Buffers paramBuffers)
  {
    this.log.info("进入deleteAccountOne方法...");
    this.outBuffer = paramBuffers;
    this.inBuffer = paramBuffers;
    int i = -1;
    String str = paramBuffers.getString("USER_ID");
    try
    {
      i = LockAccount(str);
    }
    catch (SaasApplicationException localSaasApplicationException)
    {
      this.log.info(localSaasApplicationException.getMessage());
    }
    if (i != 0)
    {
      this.outBuffer.setInt("RESULT_CODE", -1);
      this.outBuffer.setString("RESULT_INFO", "业务处理失败！");
    }
    else
    {
      this.outBuffer.setInt("RESULT_CODE", 0);
      this.outBuffer.setString("RESULT_INFO", "业务处理成功！");
    }
    this.log.info("退出deleteAccountOne方法...");
  }

  public int LockAccount(String paramString)
    throws SaasApplicationException
  {
    UserExt localUserExt = new UserExt();
    localUserExt.setParam(":VUSER_ID", paramString);
    this.tradeQuery.executeBy(localUserExt.insBy("LOCK_ACCOUNT_BY_USER_ID"));
    return 0;
  }

  public void UnLockAccount(Buffers paramBuffers)
  {
    this.log.info("进入deleteAccountOne方法...");
    this.outBuffer = paramBuffers;
    this.inBuffer = paramBuffers;
    int i = -1;
    String str = paramBuffers.getString("USER_ID");
    try
    {
      i = UnLockAccount(str);
    }
    catch (SaasApplicationException localSaasApplicationException)
    {
      this.log.info(localSaasApplicationException.getMessage());
    }
    if (i != 0)
    {
      this.outBuffer.setInt("RESULT_CODE", -1);
      this.outBuffer.setString("RESULT_INFO", "业务处理失败！");
    }
    else
    {
      this.outBuffer.setInt("RESULT_CODE", 0);
      this.outBuffer.setString("RESULT_INFO", "业务处理成功！");
    }
    this.log.info("退出deleteAccountOne方法...");
  }

  public int UnLockAccount(String paramString)
    throws SaasApplicationException
  {
    UserExt localUserExt = new UserExt();
    localUserExt.setParam(":VUSER_ID", paramString);
    this.tradeQuery.executeBy(localUserExt.insBy("UNLOCK_ACCOUNT_BY_USER_ID"));
    return 0;
  }
}

